package com.rockwell.robot2015.model.detectors;

import org.opencv.core.Point;

import com.rockwell.robot2015.model.ColorBlobInfo;
import com.rockwell.robot2015.model.OnColorBlobDetectListener;

public class MazeDetector extends ObjectDetector {
	private static final int MIN_SIZE = 20;
	
	private boolean m_detectLine;
	
	private Point[] m_line;
	
	public MazeDetector(OnColorBlobDetectListener listener) {
		super(listener);
	}
	
	@Override
	public boolean isPresent() {
		return m_present;
	}

	@Override
	public void onDetect(boolean present, ColorBlobInfo info,
			int height, int width) {
		
		if (info.boundingRect != null && info.boundingRect.width < MIN_SIZE && info.boundingRect.height < MIN_SIZE) {
			m_present = false;
			return;
		}
		
		if (!this.m_present && !present)
			return;
		
		if (this.m_present && present)
		{
			double difX = Math.abs(m_center.x - info.center.x);
			double difY = Math.abs(m_center.y - info.center.y);
			
			if (difX < 1 && difY < 1)
				return;
		}
		
		m_present = present;
		
		m_center = info.center;
		m_radius = info.radius;
		m_detectLine = info.detectLine;
		m_line = info.line;
		m_boundingRect = info.boundingRect;

		if (m_width != width)
			m_width = width;
		
		if (m_height != height)
			m_height = height;
	}

	@Override
	public boolean detectLine() {
		return m_detectLine;
	}

	@Override
	public Point[] getLine() {
		return m_line;
	}

	@Override
	public Point getDrift() {
		/*
		if (m_present && m_boundingRect != null) {
			double cx = m_boundingRect.x + m_boundingRect.width / 2;
			double cy = m_boundingRect.y + m_boundingRect.height / 2;
			
			double x = m_width - cx;
			double y = cy - m_height / 2;
			
			return new Point(x, y);
		}
		else {
			return new Point(100, 1);
		}
		*/
		if (m_present && m_center != null) {
			double x = m_width - m_center.x;
			double y = m_center.y - m_height / 2;
			
			return new Point(x, y);
		}
		else {
			return new Point(100, 1);
		}
	}
}
